<?php

namespace App\Library\Service;

use App\Library\Logic\ShopUserMessageLogic;
use App\Library\Requests\Base\BasePageRequest;
use App\Library\Requests\Base\IdRequest;
use App\Library\Requests\UserMessage\UserMessageAddRequest;
use App\Library\Requests\UserMessage\UserMessageNewTimeRequest;
use App\Library\Requests\UserMessage\UserMessageReadRequest;
use Illuminate\Http\JsonResponse;

class UserMessageService extends BaseService
{
    /**
     * @param ShopUserMessageLogic $logic
     */
    public function __construct(ShopUserMessageLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * 分页
     * @param BasePageRequest $request
     * @return JsonResponse
     */
    public function paging(BasePageRequest $request): JsonResponse
    {
        return success($this->logic->paging($request));
    }

    /**
     * add
     * @param UserMessageAddRequest $request
     * @return JsonResponse
     */
    public function add(UserMessageAddRequest $request): JsonResponse
    {
        $this->logic->insertForModel(array_merge($this->saveCom($request), [
            'userId' => USERID,
            'umSendUserId' => USERID,
            'imAddTime' => time(),
            'createdAt' => time(),
        ]));
        return success();
    }

    /**
     * detail
     * @param IdRequest $request
     * @return JsonResponse
     */
    public function detail(IdRequest $request): JsonResponse
    {
        $item = $this->logic->detail($request->id);
        if (is_null($item) || $item->um_receive_user_id !== USERID) return fail(433);
        if ($item->um_status === 2) {
            $item->um_status = 1;
            $item->save();
        }
        return success($item);
    }

    /**
     * detail
     * @param IdRequest $request
     * @return JsonResponse
     */
    public function delete(IdRequest $request): JsonResponse
    {
        $item = $this->logic->detail($request->id);
        if (is_null($item) || $item->um_receive_user_id !== USERID) return fail(433);
        $item->delete();
        return success();
    }

    /**
     * count
     * @return JsonResponse
     */
    public function count(): JsonResponse
    {
        return success($this->logic->countByWhere(['umStatus' => 2, 'umReceiveUserId' => USERID]));
    }

    /**
     * read
     * @param UserMessageReadRequest $request
     * @return JsonResponse
     */
    public function read(UserMessageReadRequest $request): JsonResponse
    {
        return success($this->logic->newQuery()->where([
            'um_receive_user_id' => USERID,
            'um_send_user_id' => $request->receiveUserId,
            'um_type' => $request->type,
            'um_status' => 2,
        ])->limit(10)->orderByDesc('um_id')->update(['um_status' => 1]));
    }

    /**
     * new
     * @param UserMessageNewTimeRequest $request
     * @return JsonResponse
     */
    public function newTime(UserMessageNewTimeRequest $request): JsonResponse
    {
        return success($this->logic->newQuery()->where([
            'um_receive_user_id' => USERID,
            'um_send_user_id' => $request->receiveUserId,
            'um_type' => $request->type,
        ])->limit(1)->orderByDesc('um_id')->update(['im_add_time' => time()]));
    }

    /**
     * new
     * @param UserMessageAddRequest $request
     * @return array
     */
    private static function saveCom(UserMessageAddRequest $request): array
    {
        $array = handleRequest($request);
        foreach ($array as $k => $v) {
            $array['um' . ucfirst($k)] = $v;
            unset($array[$k]);
        }
        $array['updatedAt'] = time();
        return $array;
    }
}
